package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.CourseGoal;
import com.cqjtu.eecs.entity.CourseGoalAssess;
import com.cqjtu.eecs.entity.Major;
import com.cqjtu.eecs.entity.dto.WeightDto;
import com.cqjtu.eecs.exception.CourseNotExistException;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.mapper.*;
import com.cqjtu.eecs.service.ICourseGoalAssessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjtu.eecs.service.ICourseService;
import com.cqjtu.eecs.service.IProjectCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 课程目标-考核环节 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@Service
public class CourseGoalAssessServiceImpl extends ServiceImpl<CourseGoalAssessMapper, CourseGoalAssess> implements ICourseGoalAssessService {

    @Autowired
    private IProjectCourseService projectCourseService;

    @Autowired
    private CourseGoalMapper courseGoalMapper;

    @Autowired
    private CourseGoalAssessMapper courseGoalAssessMapper;


    /**
     * 获取课程目标考核环节映射
     * @param grade 年级
     * @param courseName 课程名
     * @param majorName 专业名
     * @return
     */
    @Override
    public Map<Integer, Map<String, WeightDto>> getCourseGoalLinkMap(Integer grade, String courseName, String majorName) {
        // 获取方案-课程编号
        Integer pcId = projectCourseService.getProjectCourseId(grade, majorName, courseName);
        // 根据方案-课程编号获取该课程的所有课程目标
        List<CourseGoal> courseGoalList  = courseGoalMapper.selectList(new QueryWrapper<CourseGoal>().eq("project_course_id", pcId));

        Map<Integer, Map<String, WeightDto>> goalAssessMap = new HashMap<>();
        courseGoalList.forEach(courseGoal -> {
            Map<String, WeightDto> assessLinkMap = this.getAssessLinkMapByCourseGoalId(courseGoal.getCourseGoalId());
            goalAssessMap.put(courseGoal.getCourseGoalId(), assessLinkMap);
        });
        return goalAssessMap;
    }

    /**
     * 根据课程目标编号获取考核环节名及权重
     * @param courseGoalId
     * @return
     */
    private Map<String, WeightDto> getAssessLinkMapByCourseGoalId(Integer courseGoalId) {
        try {
            Map<String, WeightDto> linkWeightMap = new HashMap<>();
            List<Map<String, Object>> courseGoalAssess =  courseGoalAssessMapper.getAssessLinkMapByCourseGoalId(courseGoalId);
            for (Map<String, Object> map: courseGoalAssess) {
                String linkName = null;
                Float weight = null;
                Float linkWeight = null;

                for (Map.Entry<String, Object> entry: map.entrySet()) {
                    if ("link_name".equals(entry.getKey()))
                        linkName = String.valueOf(entry.getValue());
                    if ("weight".equals(entry.getKey()))
                        weight = Float.parseFloat(String.valueOf(entry.getValue()));
                    if ("link_weight".equals(entry.getKey()))
                        linkWeight = Float.parseFloat(String.valueOf(entry.getValue()));
                }
                WeightDto weightDto = new WeightDto(linkWeight, weight);
                linkWeightMap.put(linkName, weightDto);
            }
            return linkWeightMap;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }
}
